package com.mju;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import com.alibaba.excel.EasyExcel;
import com.mju.dao.SelectTeacherMapper;
import com.mju.model.Teacher;
import com.mju.model.dto.ChoiceTeacherDTO;
import com.mju.model.dto.DurationDTO;
import com.mju.model.dto.SelectTeacherDTO;
import com.mju.service.SelectTeacherService;
import com.mju.service.elastic.ElasticService;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.ElasticsearchException;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import co.elastic.clients.elasticsearch.core.search.Highlight;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;

@SpringBootTest
class EptTests2 {
	@Value("${xxl.job.admin.addresses}")
	private String adminAddresses;

	@Value("${xxl.job.admin.accessToken}")
	private String accessToken;

	@Value("${xxl.job.admin.timeout}")
	private int timeout;

	@Value("${xxl.job.executor.appname}")
	private String appname;

	@Value("${xxl.job.executor.address}")
	private String address;

	@Value("${xxl.job.executor.ip}")
	private String ip;

	@Value("${xxl.job.executor.port}")
	private int port;

	@Value("${xxl.job.executor.logpath}")
	private String logPath;

	@Value("${xxl.job.executor.logretentiondays}")
	private int logRetentionDays;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	@Autowired
	private SelectTeacherService selectTeacherService;

	@Autowired
	private SelectTeacherMapper selectTeacherMapper;

	@Value("${custom.choiceRedisKey}")
	private String choiceRedisKey;

	@Test
	public void testStreamReceive() {
//		StreamOperations<String, String, String> opt= redisTemplate.opsForStream();
//		
//		
//		List<Map<String,String>> list=teacherService.receiveMsg("2020", 1);
//		
//		for(Map<String,String> x:list) {
//			System.out.println(x);
//			opt.delete(choiceRedisKey, x.get("id"));
//		}
	}

	// 导出已确认Excel表
	@Test
	public void testTotal() {
		List<SelectTeacherDTO> list = selectTeacherService.selectList("");
		EasyExcel.write("D:\\students.xlsx", SelectTeacherDTO.class).sheet("Sheet1").doWrite(list);
	}

	@Test
	public void testFullTeacher() {
		// 获取所有选择教师
		List<ChoiceTeacherDTO> list = selectTeacherMapper.choicetotal("");
		// 列表删除需要用迭代器,列表中删除已经满的导师
		Iterator<ChoiceTeacherDTO> it = list.iterator();
		while (it.hasNext()) {
			ChoiceTeacherDTO x = it.next();
			if (x.getChoiceTotal() == x.getLimitNum())
				it.remove();
		}
		for (ChoiceTeacherDTO x : list)
			System.out.println(x);
	}

	
	@Autowired
	private ElasticsearchClient esClient;
	
	@Test
	public void testElastic() throws Exception {

		SearchResponse<Teacher> response = esClient.search(
				s -> s.index("paper_2025").query(
						q -> q.match(t -> t.field("memo")
							  .query("前端"))),
				Teacher.class);
		for (Hit<Teacher> t : response.hits().hits())
			System.out.println(t);
		esClient.close();		
	}
	
	@Autowired
	private ElasticService elasticService;
	@Test
	public void testElasticService() {
		try {
			//elasticService.createIndex();
			//elasticService.batchToDoc();
			List<Map<String,String>> list= elasticService.esByMemo("前端");
			System.out.println(list);
		} catch (ElasticsearchException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}